﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FTBS.Models.BLO;
using FTBS.Models.Entities;

namespace FTBS.Controllers
{
    public class CommentController : Controller
    {
        //
        // GET: /Comment/

        private CommentBLO _commentBlo = new CommentBLO();
        private FilmBLO _filmBlo = new FilmBLO();

        /// <summary>
        /// load films
        /// </summary>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Index()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            return View(_filmBlo.LoadFilms());
        }

        /// <summary>
        /// load comment by film
        /// </summary>
        /// <param name="filmId"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult LoadComments(int filmId)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            var comments = _commentBlo.LoadCommentByFilm(filmId);
            if (comments != null)
            {
                return PartialView("_Comment", comments);
            }
            else
            {
                ViewBag.comments = null;
                return RedirectToAction("Index");
            }
        }

        /// <summary>
        /// Delete comment
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Delete(int id)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            Comment comment = _commentBlo.LoadComment(id);
            var filmId = comment.FilmId;
            _commentBlo.DeleteComment(comment);
            return PartialView("_Comment", _commentBlo.LoadCommentByFilm(filmId));
        }
    }
}
